package com.hmall.ebiz.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Aspect
@Component
public class HttpAspect {
    //  使用Spring自带的Logger框架
    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @Pointcut("execution(public * com.hmall.ebiz.controller.*.*(..))")
    public void blog() {
    }

    @Before("blog()")
    public void requestBefore(JoinPoint joinPoint) {

        ServletRequestAttributes attributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        logger.info("url={}",request.getRequestURL());
        logger.info("method={}",request.getMethod());
        logger.info("ip={}", request.getRemoteAddr());
        logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() +"." + joinPoint.getSignature().getName());
        logger.info("args={}", joinPoint.getArgs());

    }

    @After("blog()")
    public void requestAfter() {
//        System.out.println("AOP 拦截后");
        logger.info("AOP 拦截后");
    }

    @AfterReturning(returning = "object", pointcut = "blog()")
    public void doAfterReturning(Object object) {
        logger.info("response={}", object);
    }
}
